package com.xintai.sparepart.spareandpart.controller;

import java.util.Iterator;
import java.util.List;

import com.xintai.common.constant.Constants;
import com.xintai.common.constant.UserConstants;
import com.xintai.common.utils.StringUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xintai.common.annotation.Log;
import com.xintai.common.core.controller.BaseController;
import com.xintai.common.core.domain.AjaxResult;
import com.xintai.common.enums.BusinessType;
import com.xintai.sparepart.spareandpart.domain.BeiTypes;
import com.xintai.sparepart.spareandpart.service.IBeiTypesService;
import com.xintai.common.utils.poi.ExcelUtil;

/**
 * 备品备件货品分类Controller
 *
 * @author xintai
 * @date 2020-12-23
 */
@RestController
@RequestMapping("/sparepart/types")
public class BeiTypesController extends BaseController
{
    @Autowired
    private IBeiTypesService beiTypesService;

    /**
     * 查询备品备件货品分类列表
     */
    @PreAuthorize("@ss.hasPermi('sparepart:types:list')")
    @GetMapping("/list")
    public AjaxResult list(BeiTypes beiTypes)
    {
        beiTypes.setTypeIsdel(Constants.SUCCESS);
        List<BeiTypes> list = beiTypesService.selectBeiTypesList(beiTypes);
        return AjaxResult.success(list);
    }

    /**
     * 查询部门列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('sparepart:types:list')")
    @GetMapping("/list/exclude/{typeId}")
    public AjaxResult excludeChild(@PathVariable(value = "typeId", required = false) Long typeId)
    {
        List<BeiTypes> types = beiTypesService.selectBeiTypesList(new BeiTypes());
        Iterator<BeiTypes> it = types.iterator();
        while (it.hasNext())
        {
            BeiTypes d = (BeiTypes) it.next();
            if (d.getTypeId().intValue() == typeId
                    || ArrayUtils.contains(StringUtils.split(d.getTypeAllName(), ","), typeId + ""))
            {
                it.remove();
            }
        }
        return AjaxResult.success(types);
    }

    /**
     * 导出备品备件货品分类列表
     */
    @PreAuthorize("@ss.hasPermi('sparepart:types:export')")
    @Log(title = "备品备件货品分类", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BeiTypes beiTypes)
    {
        List<BeiTypes> list = beiTypesService.selectBeiTypesList(beiTypes);
        ExcelUtil<BeiTypes> util = new ExcelUtil<BeiTypes>(BeiTypes.class);
        return util.exportExcel(list, "types");
    }

    /**
     * 获取备品备件货品分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('sparepart:types:query')")
    @GetMapping(value = "/{typeId}")
    public AjaxResult getInfo(@PathVariable("typeId") Long typeId)
    {
        return AjaxResult.success(beiTypesService.selectBeiTypesById(typeId));
    }

    /**
     * 新增备品备件货品分类
     */
    @PreAuthorize("@ss.hasPermi('sparepart:types:add')")
    @Log(title = "备品备件货品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BeiTypes beiTypes)
    {
        //验证是否重名
        if (UserConstants.NOT_UNIQUE.equals(beiTypesService.checkTypeNameUnique(beiTypes)))
        {
            return AjaxResult.error("新增分类'" + beiTypes.getTypeName() + "'失败，分类名称已存在");
        }
        return toAjax(beiTypesService.insertBeiTypes(beiTypes));
    }

    /**
     * 修改备品备件货品分类
     */
    @PreAuthorize("@ss.hasPermi('sparepart:types:edit')")
    @Log(title = "备品备件货品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BeiTypes beiTypes)
    {
        if (UserConstants.NOT_UNIQUE.equals(beiTypesService.checkTypeNameUnique(beiTypes))){
            return AjaxResult.error("新增分类"+beiTypes.getTypeName()+"失败，分类名称已存在");
        }else if (beiTypes.getTypeId().equals(beiTypes.getTypeParentid())){
            return AjaxResult.error("新增分类"+beiTypes.getTypeName()+"失败，上级分类不能是自己");
        }else if (StringUtils.equals(UserConstants.TYPE_DISABLE,beiTypes.getTypeIsdel())
                && beiTypesService.selectNormalChildrenTypeById(beiTypes.getTypeId()) > 0)
        {
            return AjaxResult.error("该部门包含未停用的子分类！");
        }
        return toAjax(beiTypesService.updateBeiTypes(beiTypes));
    }

    /**
     * 删除备品备件货品分类
     */
    @PreAuthorize("@ss.hasPermi('sparepart:types:remove')")
    @Log(title = "备品备件货品分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{typeIds}")
    public AjaxResult remove(@PathVariable Long typeIds)
    {
        if (beiTypesService.hasChildByTypeId(typeIds))
        {
            return AjaxResult.error("存在下级分类,不允许删除");
        }
        if (beiTypesService.checkTypeExistProducts(typeIds))
        {
            return AjaxResult.error("分类存在货品,不允许删除");
        }
        return toAjax(beiTypesService.deleteBeiTypesById(typeIds));
    }
}
